using System;
using System.Collections.Generic;
using abilities.targets;
using LitJson;

namespace abilities.triggers.conditions
{
    public class AbilityTriggerConditionTargetsAround : AbilityTriggerCondition
    {
        public TargetUnitSide TargetUnitSide;
        public float radius;
        public float minRadius;
        public int countMin;
        public int countMax;
        public TargetUnitType TargetUnitType;
        public bool includeSelf;


        public override bool FromJson(JsonData value)
        {
            string battleSide = (string)value["battleSide"];
            if ("ENEMY" == battleSide)
            {
                TargetUnitSide = TargetUnitSide.ENEMY;
            }
            else
            {
                TargetUnitSide = TargetUnitSide.ALLY;
            }

            radius = (int)value["radius"] / 100.0f;
            minRadius = (int)value["minRadius"] / 100.0f;
            countMin = value.ContainsKey("countMin") ? (int)value["countMin"] : -1;
            countMax = value.ContainsKey("countMax") ? (int)value["countMax"] : -1;
            TargetUnitType = value.ContainsKey("targetType") ? Enum.Parse<TargetUnitType>((string)value["targetType"]) : 0;

            includeSelf = (bool)value["includeSelf"];

            return true;
        }

        public override bool Check(AbilityContext context)
        {
            TargetsAround target = new(TargetUnitSide, radius, minRadius, TargetUnitType, includeSelf);
            List<TargetObject> targets = target.GetTargets(context);
            int count = targets.Count;

            bool result = true;
            if (countMin >= 0)
            {
                result = count >= countMin;
            }

            if (result && countMax >= 0)
            {
                result = count <= countMax;
            }

            return result;
        }
    }
}